package cn.yudao.org.controller;

import cn.yudao.basic.annotation.PreAuthorize;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.utils.PageList;
import cn.yudao.org.domain.Department;
import cn.yudao.org.domain.Employee;
import cn.yudao.org.query.EmployeeQuery;
import cn.yudao.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.ConsoleHandler;

@RestController
@RequestMapping("/employee")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {


    @Autowired
    private IEmployeeService service;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee loadById(@ApiParam(name="ID属性",required = true) @PathVariable Long id){
        return service.LoadById(id);
    }

    @GetMapping("/tree")
    @ApiOperation(value = "通过查询所有")
    public List<Department> loadTree(){
        return service.findTreeEmployee();
    }

    @GetMapping
    @ApiOperation(value = "通过查询所有")
    @PreAuthorize(name = "查询所有员工",sn = "employee:query")
    public List<Employee> loadAll(){
        return service.loadAll();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除所有")
    @PreAuthorize(name = "删除员工",sn = "employee:delete")
    public AjaxResult remove(@ApiParam(name="ID属性",required = true) @PathVariable Long id){
        try {
            service.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PutMapping
    @ApiOperation(value = "新增或者修改")
    @PreAuthorize(name = "新增/修改员工",sn = "employee:addOrupt")
    public AjaxResult addOrupt(@ApiParam(name="对象",required = true) @RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                service.save(employee);
            }else {
                service.upadte(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PostMapping
    @ApiOperation(value = "高级查询和分页")
    @PreAuthorize(name = "查询员工",sn = "employee:query")
    public PageList<Employee> queryDept(@ApiParam(name="查询条件和分页",required = true) @RequestBody EmployeeQuery deptQuery){
        PageList query = service.query(deptQuery);
        return service.query(deptQuery);
    }

    /**
    * @Title: patchRom
    * @Description: 批量删除
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/6/26 9:00
    * @Parameters:
    * @Return
    */
    @PatchMapping
    public AjaxResult patchRom(@RequestBody Long[] ids){
        try {
            service.patchRom(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> loadAll(@PathVariable(value = "shopId") Long shopId){

        return service.loadbyShopId(shopId);
    }

}
